﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace block_explorer
{
    class Fog : IPostProcess
    {
        private DeferredRenderer mRenderer;

        private FogEffect mEffect;

        public bool Enabled { get; set; }

        public Texture2D Surface { get { return null; } }

        public float Start { get; set; }
        public float End { get; set; }
        public Color Color { get; set; }

        public Fog(DeferredRenderer renderer)
        {
            mRenderer = renderer;
            Start = 0.0f;
            End = 1.0f;
            Color = Color.White;
        }

        public void Initialize(GraphicsDevice device, ContentManager content)
        {
            mEffect = new FogEffect(device, content);
        }

        public void PrepareScene(GraphicsDevice device)
        {
        }

        public void Draw(GraphicsDevice device)
        {
            BlendState blendState = new BlendState();
            blendState.ColorSourceBlend = Blend.SourceAlpha;
            blendState.ColorBlendFunction = BlendFunction.Add;
            blendState.ColorDestinationBlend = Blend.InverseSourceAlpha;
            device.BlendState = blendState;

            mEffect.HalfPixel = mRenderer.GBufferHalfPixel;
            mEffect.InverseProjection = Matrix.Invert(mRenderer.Scene.SelectedCamera.Projection);
            mEffect.DepthMap = mRenderer.GBuffer.Depth;
            mEffect.Start = Start;
            mEffect.End = End;
            mEffect.Color = Color;

            mEffect.Apply();
            Util.QuadRenderComponent.Instance.Render(-1 * Vector2.One, Vector2.One);
        }
    }
}
